package com.dgz.csmall.passport.controller;

import com.dgz.csmall.commons.pojo.vo.PageData;
import com.dgz.csmall.commons.web.ServiceCode;
import com.dgz.csmall.passport.pojo.param.AdminAddNewParam;
import com.dgz.csmall.passport.pojo.param.AdminLoginInfoParam;
import com.dgz.csmall.passport.pojo.vo.AdminListItemVO;
import com.dgz.csmall.commons.security.LoginPrincipal;
import com.dgz.csmall.passport.service.AdminService;
import com.dgz.csmall.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;


/**
 * @Author duguozheng
 * @Date 2023/05/29  17:52
 * @Description 处理管理员相关请求的controller控制器
 * @Version 1.0
 */

//produces:指定方法返回的数据类型和字符集。选项是可选的，如不指定，默认返回一个 HTML 响应。
@RequestMapping(value="/admins/", produces = "application/json; charset=utf-8")

@RestController
@Api(tags = "01. 管理员管理模块")
@Slf4j
@Validated
public class AdminController {

    @Autowired
    private AdminService adminService;

    // 管理员登录业务  http://localhost:9181/admin/login
    @PostMapping("login")
    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 50)
    public JsonResult login(AdminLoginInfoParam adminLoginInfoParam/*, HttpServletRequest request*/) {
        log.debug("开始处理【管理员登录】的请求，参数：{}", adminLoginInfoParam);

        /*
        String remoteAddr = request.getRemoteAddr();
        String userAgent = request.getHeader(HttpConsts.HEADER_USER_AGENT);
        AdminLoginInfoDTO loginInfoDTO = adminService.login(adminLoginInfoParam, remoteAddr, userAgent);
        log.debug("即将响应：{}", loginInfoDTO);
         */
        String jwt = adminService.login(adminLoginInfoParam);
        return JsonResult.ok(jwt);
    }

    // 添加管理员业务  http://localhost:9181/admin/add-new
    @PostMapping("add-new")
    @ApiOperation("添加管理员")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(AdminAddNewParam adminAddNewParam) {
        log.debug("开始处理【添加管理员】的请求，参数：{}", adminAddNewParam);
        adminService.addNew(adminAddNewParam);
        return JsonResult.ok();
    }

    // 查询管理员列表业务  http://localhost:9181/admin/list
    @GetMapping("list")
    @ApiOperation("查询管理员列表")
    @ApiOperationSupport(order = 420)
    public JsonResult list( @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("开始处理【查询管理员列表】的请求");
        log.debug("当事人ID：{}", loginPrincipal.getId());
        log.debug("当事人用户名：{}", loginPrincipal.getUsername());
        return JsonResult.fail(ServiceCode.ERR_UNKNOWN, "此功能尚未开发！");
    }

    // http://localhost:9181/admins
    @GetMapping("")
    @ApiOperation("查询管理员列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", dataType = "long")
    })
    public JsonResult list(Integer page) {
        log.debug("开始处理【查询管理员列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<AdminListItemVO> pageData = adminService.list(pageNum);
        return JsonResult.ok(pageData);
    }
}
